import java.lang.invoke.WrongMethodTypeException;
import java.util.Arrays;

public class MyList {
    private int[] elem;

    private int usedSize;
    private static final int DEFAULT_SIZE = 10;
    public MyList() {
        this.elem = new int[DEFAULT_SIZE];
    }
    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = data;
        usedSize++;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data)throws PosWrongfulException {
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        if(pos < 0||pos > this.usedSize){
            throw new PosWrongfulException("pos位置不合法！");
        }
        for (int i = this.usedSize-1; i >= pos ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
// 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int get(int pos) throws EmptyException, WrongMethodTypeException {
        if(isEmpty()){
            throw new EmptyException("该链表为空！");
        }
        if(pos < 0||pos >= this.usedSize){
            throw new WrongMethodTypeException("pos位置不合法！");
        }
        return this.elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) throws EmptyException, PosWrongfulException {
        if(isEmpty()){
            throw new EmptyException("该链表为空！");
        }
        if(pos < 0 ||pos >= this.usedSize){
            throw new PosWrongfulException("pos位置不合法！");
        }
        this.elem[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int key) throws EmptyException {
        if(isEmpty()){
            throw new EmptyException("该链表为空！");
        }
        int pos = this.indexOf(key);
        if(pos == -1){
            System.out.println("没有该数据");
            return;
        }
        for (int i = pos; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }
    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
    public boolean isFull(){
        return this.getUsedSize() >= this.elem.length;
    }
    public int getUsedSize() {
        return usedSize;
    }
    public boolean isEmpty(){
        if(this.usedSize == 0){
            return true;
        }else {
            return false;
        }
    }

}
